<template>
  <div>
    <div class="head">
      <div class="input">
        <span>部门名称：</span>
        <el-input v-model="queryData.company_name" placeholder="请输入部门名称" />
      </div>
      <div class="input">
        <span>状态：</span>
        <el-select v-model="queryData.status" placeholder="请选择">
          <el-option label="正常" :value="1">正常</el-option>
          <el-option label="冻结" :value="2">冻结</el-option>
        </el-select>
      </div>
      <el-button type="primary" class="btn" @click="search">查询</el-button>
      <el-button type="primary" class="btn" @click="reset">重置</el-button>
    </div>
    <div class="box">
      <div class="flex" style="margin-bottom: 15px">
        <el-button type="primary" plain icon="el-icon-plus" @click="add">新增</el-button>
        <el-button plain icon="el-icon-sort" @click="expand">展开/折叠</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        row-key="id"
        ref="tableData"
        v-loading="isTableLoading"
        empty-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
        style="width: 100%"
      >
        <el-table-column fixed prop="company_name" align="center" label="名称" min-width="200" />
        <!--<el-table-column prop="sort" align="center" label="排序" width="100" />-->
        <el-table-column prop="status" align="center" label="状态" width="120">
          <template #default="scope">
            <span class="status">
              {{ scope.row.status == 1 ? '正常' : '冻结' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" align="center" label="创建时间" width="160" />
        <el-table-column prop="domain" align="center" label="域名" min-width="160" />
        <el-table-column prop="subdomain" align="center" label="子域名" min-width="160" />
        <el-table-column prop="trainingStatus" align="center" label="培训状态" width="120">
          <template #default="scope">
            <span :style="{ color: scope.row.train_status == 1 ? '#18B127' : '' }">
              {{ scope.row.train_status == 1 ? '进行中' : scope.row.train_status == 2 ? '已到期' : '' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" prop="action" label="操作" width="160">
          <template #default="scope">
            <div class="action">
              <span @click="edit(scope)">修改</span>
              <span @click="addItem(scope)">新增</span>
              <span @click="del(scope)">删除</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <el-pagination
          style="margin-top: 5px"
          @size-change="sizeSearch"
          @current-change="search"
          :current-page.sync="queryData.page"
          :page-size.sync="queryData.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delOk">确 定</el-button>
      </span>
    </el-dialog>

    <editCompany ref="edit"> </editCompany>
  </div>
</template>

<script>
import editCompany from './edit.vue';
import request from '@/libs/request';

export default {
  name: 'cultivate',
  components: { editCompany },
  data() {
    return {
      queryData: {
        company_name: '',
        status: '',
        page: 1,
        size: 10,
      },
      total: 0,
      isTableLoading: false,
      tableData: [],
      dialogVisible: false,
      isExpand: false,
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.getList();
  },
  methods: {
    search(d) {
      if (typeof d == 'number') {
        this.queryData.page = d;
      } else {
        this.queryData.page = 1;
      }
      this.getList();
    },
    sizeSearch(d) {
      this.queryData.size = d;
      this.search();
    },
    reset() {
      this.queryData.company_name = '';
      this.queryData.status = '';
      this.search();
    },
    expand() {
      this.isExpand = !this.isExpand;
      this.toggleRowExpansionAll(this.tableData, this.isExpand);
    },
    toggleRowExpansionAll(data, isExpansion) {
      data.forEach((item) => {
        this.$refs.tableData.toggleRowExpansion(item, isExpansion);
        if (item.children !== undefined && item.children !== null) {
          this.toggleRowExpansionAll(item.children, isExpansion);
        }
      });
    },
    add() {
      this.$refs.edit.toggle();
    },
    addItem(item) {
      this.$refs.edit.toggle(item.row.id, item.row.id)
    },
    edit(item, type) {
      this.$refs.edit.toggle(item.row.id);
    },
    del(item) {
      // this.dialogVisible = true;
      this.item = item;
      this.$confirm('确定要删除企业吗？', '提醒', {
        type: 'warning',
      }).then(res => {
        this.delOk()
      }).catch(err => {
      })
    },
    delOk() {
      console.log(this.item)
      let loading = this.$loading()
      request({
        url: 'company/delete',
        method: 'post',
        data: {id: this.item.row.id}
      }).then(res => {
        loading.close()
        this.$delete(this.tableData, this.item.$index);
        this.dialogVisible = false;
        this.$message.success('删除成功')
      }).catch(err => {
        loading.close()
        this.$message.error(err.msg);
      })
    },
    getList() {
      this.isTableLoading = true;
      request({
        url: 'company/list',
        method: 'post',
        data: this.queryData
      }).then((res) => {
        this.tableData = res.data.list
        this.total = res.data.total
        this.isTableLoading = false
      }).catch((err) => {
        this.isTableLoading = false
      })
    },
  },
};
</script>

<style scoped>
.head {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px 20px 0;
}

.input {
  display: flex;
  align-items: center;
  margin-right: 15px;
  margin-bottom: 20px;
}

.input > span {
  font-size: 12px;
  flex-shrink: 0;
}

.btn {
  margin-bottom: 20px;
}

.box {
  width: 100%;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
  margin-top: 20px;
}

.action {
  display: flex;
  align-items: center;
  justify-content: center;
}

.action span {
  color: #0256ff;
  font-size: 12px;
  cursor: pointer;
  margin: 0 8px;
}

.flex {
  width: 100%;
  display: flex;
  align-items: center;
}

.status {
  padding: 4px 8px;
  background: #e8f4ff;
  line-height: 14px;
  color: #0256ff;
  border: 1px solid #84adff;
  border-radius: 4px;
}
</style>
